﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Modules._2DoTasks.Components;
using DotNetNuke.Modules._2DoTasks.Components.Controllers;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace DotNetNuke.Modules._2DoTasks
{
    public partial class ProjectMenu : _2DoTasksModuleBase
    {
        #region "Events"

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    FillControls();
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void rpt_projects_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    int projectId = (int)DataBinder.Eval(e.Item.DataItem, "ProjectId");

                    string url = Globals.NavigateURL(string.Empty, string.Empty, projectId == Null.NullInteger ? string.Empty : String.Format("pid={0}", projectId));

                    var lnk_callProject = e.Item.FindControl("lnk_callProject") as LinkButton;
                    lnk_callProject.CommandArgument = url;

                    if (ProjectId.HasValue && ProjectId.Value == projectId)
                    {
                        lnk_callProject.CssClass = "_2DoTasks_ProjectMenuItemSelected";
                    }
                }
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// <summary>
        /// Makes a call to the project id selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnk_callProject_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect((sender as LinkButton).CommandArgument);
        }

        #endregion "Events"

        #region "Aux Functions"

        /// <summary>
        /// Fills module controls
        /// </summary>
        private void FillControls()
        {
            //fill existing projects
            using (UnitOfWork uof = new UnitOfWork(PortalId, ModuleId))
            {
                ProjectController ProjectController = new ProjectController(uof);

                var projects =
                    ProjectController.GetAll().Select(p => new
                {
                    ProjectId = p.ProjectId,
                    ProjectName = p.Name
                }).ToList();

                projects.Insert(0, new { ProjectId = Null.NullInteger, ProjectName = Localization.GetString("AllProjects", this.LocalResourceFile) });

                rpt_projects.DataSource = projects;
                rpt_projects.DataBind();
            }
        }

        #endregion "Aux Functions"

        #region Optional Interfaces

        #endregion Optional Interfaces
    }
}